package com.sip.stream.utils.zfy.encoder;

import android.util.Log;

import com.sip.stream.media.ZFYCameraParameters;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;

import java.lang.ref.WeakReference;

public class ZFYEncoderFactory {
    private static final String TAG = "ZFYEncoderFactory";


    private GSAACASyncEncoder aacASyncEncoder;
    private WeakReference<IGSVideoEncoder> frontVideoEncoder;
    private WeakReference<IGSVideoEncoder> localVideoEncoder;
    private WeakReference<IGSVideoEncoder> remoteVideoEncoder;

    public static int getBitRate() {
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        ZFYCameraParameters cameraParameters = ZFYMediaManager.getInstance().getCameraParameters();
        return getBitRate(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight(), config.getVideoFPS(), config.getVideoMinetype(), config.getVideoBitType());
    }

    public static int getBitRate(int i, int i2, int i3, String str, int i4) {
        int i5 = (int) (i * i2 * i3 * (str.equalsIgnoreCase("video/hevc") ? i4 != 1 ? i4 != 2 ? i4 != 3 ? 0.06f : 0.1f : 0.02f : 0.035f : i4 != 1 ? i4 != 2 ? i4 != 3 ? 0.12f : 0.2f : 0.04f : 0.07f));
        if (i2 > 1600) {
            i5 = (int) (i5 * 0.8d);
        }
        return i3 > 30 ? (int) (i5 * 0.7d) : i5;
    }

    public synchronized IGSVideoEncoder getMp4VideoEncoder()
    {
        WeakReference<IGSVideoEncoder> weakReference = this.localVideoEncoder;
        if (weakReference != null && weakReference.get() != null) {
            if (this.localVideoEncoder.get().isStop()) {
                this.localVideoEncoder.clear();
                this.localVideoEncoder = null;
                Log.i(TAG, "local VideoEncoder clear");
            } else {
                return this.localVideoEncoder.get();
            }
        }
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        ZFYCameraParameters cameraParameters = ZFYMediaManager.getInstance().getCameraParameters();
        int bitRate = getBitRate(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight(), config.getVideoFPS(), config.getVideoMinetype(), config.getVideoBitType());
        if (config.getVideoMinetype().equalsIgnoreCase("video/hevc")) {
            this.localVideoEncoder = new WeakReference<>(new GSH265ASyncEncoder(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight(), bitRate, ZFYMediaManager.getInstance().getConfig().getVideoFPS(), 1));
        } else {
            this.localVideoEncoder = new WeakReference<>(new GSH264ASyncEncoder(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight(), bitRate, ZFYMediaManager.getInstance().getConfig().getVideoFPS(), 1));
        }
        return this.localVideoEncoder.get();
    }

    public IGSVideoEncoder getPlatformVideoEncoder(int width, int height, int frameRate) {
        IGSVideoEncoder gSH264ASyncEncoder;
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        if (!config.isDualStream()) {
            Log.i(TAG, "not DualStream use local encoder");
            WeakReference<IGSVideoEncoder> weakReference = new WeakReference<>(getMp4VideoEncoder());
            this.remoteVideoEncoder = weakReference;
            return weakReference.get();
        }
        WeakReference<IGSVideoEncoder> weakReference2 = this.remoteVideoEncoder;
        if (weakReference2 != null && weakReference2.get() != null) {
            if (this.remoteVideoEncoder.get().isStop()) {
                this.remoteVideoEncoder.clear();
                Log.i(TAG, "remoteVideoEncoder clear");
                this.remoteVideoEncoder = null;
            } else {
                return this.remoteVideoEncoder.get();
            }
        }
        int streamBitRate = config.getStreamBitRate() * 1024;
        if (config.getStreamMinetype().equalsIgnoreCase("video/hevc")) {
            gSH264ASyncEncoder = new GSH265ASyncEncoder(width, height, streamBitRate, frameRate, 1);
        } else {
            gSH264ASyncEncoder = new GSH264ASyncEncoder(width, height, streamBitRate, frameRate, 1);
        }
        this.remoteVideoEncoder = new WeakReference<>(gSH264ASyncEncoder);
        return gSH264ASyncEncoder;
    }

    public IGSVideoEncoder getGroupIntercomVideoEncoder() {
        IGSVideoEncoder gSH264ASyncEncoder;
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        if (!config.isDualStream()) {
            Log.i(TAG, "not DualStream use local encoder");
            WeakReference<IGSVideoEncoder> weakReference = new WeakReference<>(getMp4VideoEncoder());
            this.remoteVideoEncoder = weakReference;
            return weakReference.get();
        }
        WeakReference<IGSVideoEncoder> weakReference2 = this.remoteVideoEncoder;
        if (weakReference2 != null && weakReference2.get() != null) {
            if (this.remoteVideoEncoder.get().isStop()) {
                this.remoteVideoEncoder.clear();
                Log.i(TAG, "remoteVideoEncoder clear");
                this.remoteVideoEncoder = null;
            } else {
                return this.remoteVideoEncoder.get();
            }
        }
        int streamBitRate = ZFYMediaManager.getInstance().getConfig().getStreamBitRate() * 1024;
        if (config.getStreamMinetype().equalsIgnoreCase("video/hevc")) {
            gSH264ASyncEncoder = new GSH265ASyncEncoder(ZFYMediaManager.getInstance().getCameraParameters().getRemoteWidth(), ZFYMediaManager.getInstance().getCameraParameters().getRemoteHeight(), streamBitRate, ZFYMediaManager.getInstance().getConfig().getStreamFPS(), 1);
        } else {
            gSH264ASyncEncoder = new GSH264ASyncEncoder(ZFYMediaManager.getInstance().getCameraParameters().getRemoteWidth(), ZFYMediaManager.getInstance().getCameraParameters().getRemoteHeight(), streamBitRate, ZFYMediaManager.getInstance().getConfig().getStreamFPS(), 1);
        }
        this.remoteVideoEncoder = new WeakReference<>(gSH264ASyncEncoder);
        return gSH264ASyncEncoder;
    }

    public IGSVideoEncoder getFakeVideoEncode() {
        return new GSFakeVideoEncoder();
    }

    public IGSAudioEncoder getMp4AudioEncoder() {
        GSAACASyncEncoder gSAACASyncEncoder = this.aacASyncEncoder;
        if (gSAACASyncEncoder != null) {
            if (!gSAACASyncEncoder.isStop) {
                Log.i(TAG, "dual use getMp4AudioEncoder");
                return this.aacASyncEncoder;
            }
            this.aacASyncEncoder = null;
        }
        GSAACASyncEncoder gSAACASyncEncoder2 = new GSAACASyncEncoder(ZFYMediaManager.getInstance().getConfig().getAudioSampleRate(), ZFYMediaManager.getInstance().getConfig().getAudioChanelCount(), 131072);
        this.aacASyncEncoder = gSAACASyncEncoder2;
        return gSAACASyncEncoder2;
    }

    public void release() {
        WeakReference<IGSVideoEncoder> weakReference = this.localVideoEncoder;
        if (weakReference != null) {
            weakReference.clear();
            this.localVideoEncoder = null;
        }
        WeakReference<IGSVideoEncoder> weakReference2 = this.remoteVideoEncoder;
        if (weakReference2 != null) {
            weakReference2.clear();
            this.remoteVideoEncoder = null;
        }
    }

    public void setRemotePerformanceLevel(int i) {
        WeakReference<IGSVideoEncoder> weakReference = this.remoteVideoEncoder;
        if (weakReference != null && weakReference.get() != null) {
            this.remoteVideoEncoder.get().setPerformanceLevel(i);
            return;
        }
        WeakReference<IGSVideoEncoder> weakReference2 = this.localVideoEncoder;
        if (weakReference2 == null || weakReference2.get() == null) {
            return;
        }
        this.localVideoEncoder.get().setPerformanceLevel(i);
    }

    public synchronized IGSVideoEncoder getFrontVideoEncoder(ZFYCameraParameters zFYCameraParameters) {
        WeakReference<IGSVideoEncoder> weakReference = this.frontVideoEncoder;
        if (weakReference != null && weakReference.get() != null) {
            if (this.frontVideoEncoder.get().isStop()) {
                this.frontVideoEncoder.clear();
                this.frontVideoEncoder = null;
                Log.i(TAG, "local VideoEncoder clear");
            } else {
                return this.frontVideoEncoder.get();
            }
        }
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        long videoWidth = zFYCameraParameters.getVideoWidth() * zFYCameraParameters.getVideoHeight() * ZFYMediaManager.getInstance().getConfig().getVideoFPS();
        float f = 0.9f;
        if (config.getVideoMinetype().equalsIgnoreCase("video/hevc")) {
            int videoBitType = config.getVideoBitType();
            if (videoBitType == 1) {
                f = 0.035f;
            } else if (videoBitType != 2) {
                f = 0.07f;
            }
            int i = (int) (((float) videoWidth) * f);
            if (zFYCameraParameters.getVideoHeight() > 1600) {
                i = (int) (i * 0.8d);
            }
            this.frontVideoEncoder = new WeakReference<>(new GSH265ASyncEncoder(zFYCameraParameters.getVideoWidth(), zFYCameraParameters.getVideoHeight(), i, ZFYMediaManager.getInstance().getConfig().getVideoFPS(), 1));
        } else {
            int videoBitType2 = config.getVideoBitType();
            float f2 = videoBitType2 != 1 ? videoBitType2 != 2 ? 0.14f : 0.04f : 0.07f;
            int i2 = (int) (((float) videoWidth) * f2);
            if (zFYCameraParameters.getVideoHeight() > 1600) {
                i2 = (int) (i2 * 0.7d);
            }
            this.frontVideoEncoder = new WeakReference<>(new GSH264ASyncEncoder(zFYCameraParameters.getVideoWidth(), zFYCameraParameters.getVideoHeight(), i2, ZFYMediaManager.getInstance().getConfig().getVideoFPS(), 1));
        }
        return this.frontVideoEncoder.get();
    }
}
